package tech.konata.styleslite.rendering.shaders;

import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.pipeline.TextureTarget;
import com.mojang.blaze3d.shaders.Uniform;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.vehicle.Minecart;
import org.lwjgl.opengl.GL30;

import java.awt.*;

public class RectangleShader extends GlProgram {

    private Uniform uSize;
    private Uniform uLocation;
    private Uniform radius;
    private Uniform color1;
    private Uniform color2;
    private Uniform color3;
    private Uniform color4;

    private RenderTarget input;

    public RectangleShader() {
        super(new ResourceLocation("konata", "rectangle"), DefaultVertexFormat.POSITION);
    }

    // tricks lmao
    private final float ONE_DIVIDE_BY_255 = 1 / 255.0f;

    public void setParameters(float x, float y, float width, float height, float r, Color color) {
        float i = (float) mc.getWindow().getGuiScale();
        radius.set(r * i);
        uLocation.set(x * i, -y * i + mc.getWindow().getGuiScaledHeight() * i - height * i);
        uSize.set(width * i, height * i);


        color1.set(color.getRed() * ONE_DIVIDE_BY_255, color.getGreen() * ONE_DIVIDE_BY_255, color.getBlue() * ONE_DIVIDE_BY_255, color.getAlpha() * ONE_DIVIDE_BY_255);
        color2.set(color.getRed() * ONE_DIVIDE_BY_255, color.getGreen() * ONE_DIVIDE_BY_255, color.getBlue() * ONE_DIVIDE_BY_255, color.getAlpha() * ONE_DIVIDE_BY_255);
        color3.set(color.getRed() * ONE_DIVIDE_BY_255, color.getGreen() * ONE_DIVIDE_BY_255, color.getBlue() * ONE_DIVIDE_BY_255, color.getAlpha() * ONE_DIVIDE_BY_255);
        color4.set(color.getRed() * ONE_DIVIDE_BY_255, color.getGreen() * ONE_DIVIDE_BY_255, color.getBlue() * ONE_DIVIDE_BY_255, color.getAlpha() * ONE_DIVIDE_BY_255);
    }

    public void setParameters(float x, float y, float width, float height, float r, float alpha, Color c1, Color c2, Color c3, Color c4) {
        int i = mc.options.guiScale;
        radius.set(r * i);
        uLocation.set(x * i, -y * i + mc.getWindow().getGuiScaledHeight() * i - height * i);
        uSize.set(width * i, height * i);
        color1.set(c1.getRed() * ONE_DIVIDE_BY_255, c1.getGreen() * ONE_DIVIDE_BY_255, c1.getBlue() * ONE_DIVIDE_BY_255, alpha);
        color2.set(c2.getRed() * ONE_DIVIDE_BY_255, c2.getGreen() * ONE_DIVIDE_BY_255, c2.getBlue() * ONE_DIVIDE_BY_255, alpha);
        color3.set(c3.getRed() * ONE_DIVIDE_BY_255, c3.getGreen() * ONE_DIVIDE_BY_255, c3.getBlue() * ONE_DIVIDE_BY_255, alpha);
        color4.set(c4.getRed() * ONE_DIVIDE_BY_255, c4.getGreen() * ONE_DIVIDE_BY_255, c4.getBlue() * ONE_DIVIDE_BY_255, alpha);
    }

    @Override
    public void use() {
        var buffer = mc.getMainRenderTarget();

        if (input.width != buffer.width || input.height != buffer.height)
            input.resize(buffer.width, buffer.height, Minecraft.ON_OSX);

        input.bindWrite(false);
        GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, buffer.frameBufferId);
        GL30.glBlitFramebuffer(0, 0, buffer.width, buffer.height, 0, 0, buffer.width, buffer.height, GL30.GL_COLOR_BUFFER_BIT, GL30.GL_LINEAR);
        buffer.bindWrite(false);
        super.use();
    }

    @Override
    protected void setup() {
        uSize = findUniform("uSize");
        uLocation = findUniform("uLocation");
        radius = findUniform("radius");
        color1 = findUniform("color1");
        color2 = findUniform("color2");
        color3 = findUniform("color3");
        color4 = findUniform("color4");
        var window = mc.getWindow();
        input = new TextureTarget(window.getWidth(), window.getHeight(), false, Minecraft.ON_OSX);
    }
}
